package j.todo;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class totlist extends ListActivity {
	static final  String C0L1 = "line";
	private SimpleCursorAdapter adapter;	
	DBAdapter db;

	private TextView text;
	private EditText editMod;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    	setContentView(R.layout.totlist);
	    // TODO Auto-generated method stub
	    	((Button)findViewById(R.id.btnMod)).setOnClickListener(listener);
	        
	    	db = new DBAdapter(this);
	    	db.open();
			text = (TextView)findViewById(R.id.textTot);; //  Todo list number
			editMod = (EditText)findViewById(R.id.editTextMod);    // input 


		

	}
	
	Button.OnClickListener listener = new View.OnClickListener(){
		public void onClick(View view){
			String id = text.getText().toString();
			String str = editMod.getText().toString();
			
			if(str.length()!=0){
			db.modMemo(id, str);
			Toast.makeText(getBaseContext(),  str + "수정완료!", Toast.LENGTH_LONG).show();
			
			Cursor c = db.fetchAllMemo();
			startManagingCursor(c);
			adapter.changeCursor(c);
			}

		}
		
	};
	
	protected void onResume(){
		super.onResume();

		Cursor c = db.fetchAllMemo();
		Cursor cursor = db.fetchAllMemo();
		startManagingCursor(c);
		
		String[] from = new String[] { BaseColumns._ID, C0L1};
		int[] to = new int[] { R.id.id, R.id.memo};
		
		
		adapter = new SimpleCursorAdapter(
				this, R.layout.row, c,from, to);  //
		setListAdapter(adapter);
		
	}
	protected void onPause(){
		super.onPause();
		db.close();
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l,v,position,id);
		
		final TextView t = (TextView)v.findViewById(R.id.memo);
		editMod.setText(t.getText());
		text.setText(Long.toString(id));
		
		
	
	}
	

}
